
<template>
    <div style="height: 100%;">
        <m-page-list
                :option="option"
                :page="page"
                :total="total"
                :table-data="tableData"
                :table-loading="tableLoading"
                :page-loading="pageLoading"
                v-model:value="query"
                @h-btn-click="hBtnClick"
                @col-btn-click="colBtnClick"
                @search-data="searchData"
        />
        <m-dialog-form ref="dialogRef" :data="dialogData" v-model:show="dialogShow" :option="dialogOption" @confirm="dialogConfirm" ></m-dialog-form>
    </div>
</template>

<script setup>

  import {ref,reactive,onMounted,nextTick} from 'vue'
  //组件
  import MPageList from '@/components/PageList'
  import MDialogForm from '@/components/DialogForm/DialogForm'
  //请求
  import {getLeaveRegistration, addLeaveRegistration, editLeaveRegistration, delLeaveRegistration,exportLeaveRegistration} from '@/request/api/basicData'
  import {downFile} from "../../utils/utils";
  import {refOpt} from "../const";

  import {usePageList} from "../../composables/pageList";

  //格式化编辑时显示的数据
  const formatShowData = (data,row) =>{
    let arr = [];
    row.is_leave_on_leaveschool && arr.push('is_leave_on_leaveschool');
    row.is_leave_on_gotoschool && arr.push('is_leave_on_gotoschool');
    data.is_leave_on_gotoschool = arr;
  }
  //格式化提交数据时的数据
  const formatSubmitData = (data) =>{
    let arr = [...data.is_leave_on_gotoschool];
    data.is_leave_on_leaveschool = arr.includes('is_leave_on_leaveschool');
    data.is_leave_on_gotoschool = arr.includes('is_leave_on_gotoschool');
  }

  let opts = {
    auth_id: '',
    getApi: getLeaveRegistration,addApi:addLeaveRegistration,editApi:editLeaveRegistration,delApi:delLeaveRegistration,
    formatShowData,formatSubmitData,
    idKey: 'row_id',
    total: [
      {title: '去程请假',field_id:'leave_on_gotoschool_count', value: 0},
      {title: '回程请假',field_id:'leave_on_leaveschool_count', value: 0},
    ],
    totalField: 'footer'
  };
  let {option,page,total,tableLoading,pageLoading,tableData,query,dialogRef,dialogShow,dialogData,dialogOption,
    newData,rowEdit,hBtnClick,colBtnClick,searchData,getDataList,dialogConfirm} = usePageList(opts);

  option.hLBtns = [];
  option.hRBtns = [
    {title: '新增',id: 'newData',type: 'primary',event: newData},
    {title: '导出',id: '',type: 'primary', event: exportAfterPay}
    ]
  option.conditions = [
    {label: '订单编号',type: 'text',field_id: 'out_trade_no',col: 12},
    {label: '乘车人',type: 'text',field_id: 'passengers_name',col: 12},
    {label: '请假日期',type: 'daterange',field_id: ['leave_date_begin','leave_date_end'],length: 'YYYY-MM-DD',col: 12},
    {label: '登记时间',type: 'datetimerange',field_id: ['check_in_date_begin','check_in_date_end'],length: 'YYYY-MM-DD HH:mm',col: 12},
  ];
  option.cols = [
    {prop: 'out_trade_no', label: '订单编号',},
    {prop: 'leave_date', label: '请假日期',},
    {prop: 'is_leave_on_gotoschool',type:'bool', label: '去程请假',},
    {prop: 'is_leave_on_leaveschool',type:'bool', label: '回程请假',},
    {prop: 'passengers_name', label: '乘车人',},
    {prop: 'customer_name',label: '客户名称' },
    {prop: 'phone',label: '联系电话' },
    {prop: 'school_name',label: '站点名称' },
    {prop: 'class_',label: '班级' },
    {prop: 'student_id',label: '乘客学号' },
    {prop: 'identification_no',label: '证件号码' },
    {prop: 'abstract_',label: '摘要' },
    {prop: 'input_id',label: '录入员号' },
    {prop: 'input_name',label: '录入员名' },
    {prop: 'input_time',label: '录入时间' },
    {prop: 'modify_id',label: '修改员号' },
    {prop: 'modify_name',label: '修改员名' },
    {prop: 'modify_time',label: '修改时间' },
  ];
  dialogOption.fieldList = [
    {field_id: 'out_trade_no',label: '订单编号',type: 'text',col: 3 ,editDisabled: true,required: true},
    {...refOpt.customer_name,col: 3,editDisabled: true,required: true},
    {field_id: 'student_id',label: '乘客学号',type: 'text',col: 3 ,required: true},
    {field_id: 'identification_no',label: '证件号码',type: 'text',col: 3 ,required: true},
    {...refOpt.school_name,col: 3,required: true},
    {field_id: 'class_',label: '班级',type: 'text',col: 3 ,required: true},
    {field_id: 'leave_date',label: '请假日期',type: 'date',dateFormat: 'YYYY-MM-DD',col: 3 ,required: true},
    {field_id: 'is_leave_on_gotoschool',type: 'checkbox',col: 3,options: [{name:'去程',id: 'is_leave_on_gotoschool'},{name: '回程',id: 'is_leave_on_leaveschool'}]},
    {field_id: 'abstract_',label: '摘要',type: 'text',col: 3  },
  ];

  option.showTotal = true;

  //导出
  function exportAfterPay () {
    pageLoading.value = true;
    exportLeaveRegistration(query.value).then(res =>{
      pageLoading.value = false;
      let {data} = res;
      let filename = decodeURIComponent(res.headers["content-disposition"]);
      filename = filename?.split(`filename*=UTF-8''`)[1]
      downFile(data,filename);
    })
  }

</script>

<style scoped>

</style>
